<template>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	var _codeQueryTag = false;
	export default {
		props: {
			value: {
				type: String
			}
		},
		data() {
			return {
				activity: null,
				receiver: null,
				intentFilter: null,
				context: null,
				intent: null,
				flag: false,
			}
		},
		computed: {
			...mapState('user', ['userInfo'])
		},
		created: function(option) {
			//#ifdef APP-PLUS
			this.initScan()
			this.startScan();
			// #endif
			
		},
		onHide: function() {
			this.stopScan();
		},
		destroyed: function() {
			//页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果/
			this.stopScan();
			console.log('xiezai');
		},
		methods: {
			initScan() {
				let device_model = uni.getSystemInfoSync().model; //手机类型
				let _this = this;
				this.activity = plus.android.runtimeMainActivity(); //获取activity
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				this.intentFilter = new IntentFilter();
				let Action, Extras, Special = false;
				if ("C6000" == device_model) {
					Action = "com.android.scancontext";
					Extras = "Scan_context";
				} else if ("BN-HH-G02" == device_model) {
					Action = "SYSTEM_BAR_READ";
					Extras = "BAR_VALUE";
				} else if ("fise82_wet_kk" == device_model) {
					Action = "cs2c.com.cn.serialscan";
					Extras = "serial";
				} else if ("Glory50" == device_model) {
					Action = "com.honeywell.tools.action.scan_result";
					Extras = "decode_rslt";
				} else if ("ScanPal EDA50" == device_model ||
					"EDA50P" == device_model) {
					Action = "com.honeywell.tools.action.scan_result";
					Extras = "decode_rslt";
				} else if ("EDA51" == device_model) {
					Action = "com.honeywell.tools.action.scan_result";
					Extras = "decode_rslt";
				} else if ("SQ50" == device_model) {
					Action = "android.intent.ACTION_DECODE_DATA";
					Extras = "barcode_string";
					Special = true;
				} else if ("SQ51" == device_model) {
					Action = "android.intent.ACTION_DECODE_DATA";
					Extras = "barcode_string";
					Special = true;
				} else if ("i6200 Series" == device_model) {
					Action = "urovo.rcv.message";
					Extras = "barocode";
					Special = true;
				} else if ("Camus" == device_model) {
					Action = "com.neusoft.action.scanner.read";
					Extras = "scanner_value";
				} else if ("ISAAC" == device_model) {
					Action = "com.neusoft.action.scanner.read";
					Extras = "scanner_value";
				} else if ("JOYREE Z7" == device_model) {
					Action = "android.intent.action.BARCODEDATA";
					Extras = "barcode_result";
				} else if ("NLS-MT90" == device_model) {
					Action = "nlscan.action.SCANNER_RESULT";
					Extras = "SCAN_BARCODE1";
				} else if ("H802" == device_model ||
					"H702" == device_model ||
					"K901" == device_model) {
					Action = "com.android.receive_scan_action";
					Extras = "data";
				} else if ("m92" == device_model || "m82" == device_model) {
					Action = "com.android.decodewedge.decode_action";
					Extras = "com.android.decode.intentwedge.barcode_string";
				} else if ("Android Handheld Terminal" == device_model) {
					Action = "com.barcode.sendBroadcast";
					Extras = "BARCODE";
				} else if ("NLS-MT95-5G" == device_model) {
					Action = "nlscan.action.SCANNER_RESULT";
					Extras = "SCAN_BARCODE1";
				} else {
					Action = "com.android.scancontext";
					Extras = "Scan_context";
				}
				this.intentFilter.addAction(Action) // 换你的广播动作

				this.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						// uni.showToast({
						// 	title:'接收到广播',
						// 	duration:50000
						// })
						_this.context = context
						_this.intent = intent
						plus.android.importClass(intent);
						let content = intent.getStringExtra(Extras); // 换你的广播标签
						// let content = intent.getStringExtra('data'); // 换你的广播标签
						let containSpecials = _this.containSpecial(content)

						if (containSpecials) {
							let ScanResult = content.split(",")[1].toString().trim();


							if (_this.userInfo?.sUserid && _this.userInfo?.sPassword) {
								_this.$emit('scanCodeId', content, _this.value)

							} else {
								_this.queryCode(ScanResult);
							}

						} else {

							_this.$emit('scanCodeId', content, _this.value)
						}
					}
				});
			},
			startScan() {
				this.activity.registerReceiver(this.receiver, this.intentFilter);
			},
			stopScan() {
				this.activity.unregisterReceiver(this.receiver);
			},
			containSpecial(s) {
				var containSpecial = RegExp(
					/(\,)/
				);
				return (containSpecial.test(s));
			},
			queryCode: function(code) {
				//防重复  
				if (_codeQueryTag) return false;
				_codeQueryTag = true;
				setTimeout(function() {
					_codeQueryTag = false;
				}, 150);
				uni.$emit('scancodedate', code)
			}
		}
	}
</script>

<style>
</style>
